<?php
class smarty_ext {

	function smarty_ext() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}
	
	function set_ext2 ($ext) {
		$this->ext2=$ext;
	}
	
	function get_lang_status($params){
		global $treeclass;
		return $treeclass->active_langs;
	}
	
	//function get_all_votes_with_result(){
	function get_all_votes_with_result($params){
		global $treeclass;
		
		
		$result = array();
		if( $params['lang'] ) {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type`='block_vote' AND `lang`='".$params['lang']."' ", __LINE__, __FILE__);			
		} else {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type`='block_vote'", __LINE__, __FILE__);	
		}
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['id']]['type'] = $row['type'];
			$tmp = $treeclass->get_item_content($row['id']);
			$result[$row['id']]['name'] = $tmp['block_vote_question']['value'];
			$result[$row['id']]['data'] = $tmp['block_vote_data']['value'];
		}
		return $result;
	}
	
	function get_comments_fields($params){
		$rows = array();
		if (!isset($params['id'])) return $rows;
		global $treeclass;
		return $treeclass->get_comments_fields($params['id']);
	}

	function get_comments_by_nr($params){
		$rows = array();
		if (!isset($params['nr']) or !isset($params['id'])) return $rows;
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `parent`=".$params['id']." AND `nr`= ".$params['nr']." ORDER BY `nr`", __LINE__, __FILE__);			
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$row['value'] = stripslashes($row['value']);
			$rows[] = $row;
		}
		return $rows;
	}

	function get_comment_cenzure($params){
		if ((!isset($params['nr'])) || (!isset($params['id']))) return "0";
		$sql = $this->mysqlclass->mysql_my_query("SELECT `cenzure` FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `parent`=".$params['id']." AND `nr`=".$params['nr'], __LINE__, __FILE__);		
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			if ($row['cenzure'] == 1) return "1";
			else return "0";
		} else return "0";
	}
	
	function get_comments($params){
		$rows = array();
		if (!isset($params['id'])) return $rows;
		global $treeclass;
		$site_settings = $treeclass->_get_site_settings();
		if ((isset($site_settings['site_settings_comments_cenzure'])) && ($site_settings['site_settings_comments_cenzure'] == 'on')){
			if (!isset($params['admin'])) $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `parent`=".$params['id']." AND `cenzure`=1 ORDER BY `nr`", __LINE__, __FILE__);
			else $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `parent`=".$params['id']." ORDER BY `nr`", __LINE__, __FILE__);
		} else $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."comments_data` WHERE `parent`=".$params['id']." ORDER BY `nr`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$rows[$row['nr']][$row['name']] = stripslashes($row['value']);
		}
		return $rows;
	}
	
	function is_have_comments($params){
		if (!isset($params['type'])) return 0;
		$sql1 = $this->mysqlclass->mysql_my_query("select `id` FROM `".$this->setupdata['table_prefix']."discribe` WHERE `type`='".$params['type']."' AND `group`='comment'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql1);
		if ($row == 0) return 0;
		else return 1;
	}
	
	function get_page_permision($params){
		if (!isset($params['id'])) return 0;
		if (!isset($params['user'])) return 0;
		global $treeclass;
		$res = $treeclass->get_page_permision($params['user'], $params['id']);
		return $res;
	}
	
	function get_page_permision_advanced($params){
		if (!isset($params['user'])) return 0;
		$res = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `page_id` FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id`=".$params['user']." AND `module`=''", __LINE__, __FILE__);
		while($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
      $res[$row["page_id"]] = 1;
    }
		return $res;
	}
	
	function get_site_permision($params){
		global $usersclass;
		$tmp = $this->setupdata['vars']['main_module'];
		if (isset($this->setupdata['modules']) && is_array($this->setupdata['modules'])){
			foreach($this->setupdata['modules'] as $key=>$value) if (isset($value['is']) &&($value['is'] == 1)) array_push($tmp, $key);
		}
		foreach($tmp as $value)	$site_module[$value] = 0;
		if (!isset($params['user']) || ((intval($params['user'])) == 0)) return $site_module;
		$user_info = $usersclass->GetUserInfo($params['user'], "admins");
		if (($params['user'] == 9999999) || (isset($user_info['mod']['1']) && ($user_info['mod']['1'] == 1))) {
			foreach($tmp as $value)	$site_module[$value] = 1;
			return $site_module;
		}
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id`='".$params['user']."' && `module`!=''", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			if (array_key_exists($row['module'], $site_module)){
				$site_module[$row['module']] = 1;
			}
		}
		return $site_module;
	}
	
	function class_function($params){
		if (!(isset($params['class'])) && !(isset($params['func']))) return false;
		$class = "smarty_ext_".$params['class'];
		if (isset($params['func'])) $func = $params['func'];
		else $func = "";
		if (isset($params['module'])) $module = $params['module'];
		global $$class;
		if (isset($module) && (strlen($module) > 0)){
			if (isset($this->setupdata['modules'][$params['class']]['modules'][$module])){
				if ($this->setupdata['modules'][$params['class']]['modules'][$module] == 1) return 1;
				elseif (is_array($this->setupdata['modules'][$params['class']]['modules'][$module]) && count($this->setupdata['modules'][$params['class']]['modules'][$module])>0) return 1;
				else return 0;
			} else return 0;
		} else {
			if (strlen($func) > 0){
				unset($params['class']);
				unset($params['func']);
				unset($params['module']);
				if (class_exists($class) && method_exists($$class, $func)) return eval("return \$$class -> $func(\$params);");
				else return false;
			} else {
				if (class_exists($class)) return 1;
				else return 0;
			}
		}
	}
	
	function group_has_child($params){
		$params['name'] = addslashes($params['name']);
        $params['group'] = addslashes($params['group']);
		$sql1 = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `groupe`='".$params['name']."' && `lang`='".$params['lang']."'", __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_num_rows($sql1);
		if ($row == 0) return 0;
		else {
			$info = $this->mysqlclass->mysql_my_fetch_array($sql1, MYSQL_ASSOC);
			$sql1 = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$info['parent']." && `groupe`='".$params['group']."'", __LINE__, __FILE__);
			$xdata = $this->mysqlclass->mysql_my_fetch_assoc($sql1);
			$row = $this->mysqlclass->mysql_my_num_rows($sql1);
			if ($row == 1) {						
				return $info;
			}
			else return 0;
		}
	}
	
	function is_form_block(){
		$sql1 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."discribe` WHERE `type_of_value`='block_form'", __LINE__, __FILE__);
		$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."form_post_catalog` LIMIT 1", __LINE__, __FILE__);
		if (($this->mysqlclass->mysql_my_num_rows($sql1) > 0)||($this->mysqlclass->mysql_my_num_rows($sql2) > 0)) return 1;
		else return 0;
	}
	
	function set_forms_visible($params){
		global $treeclass;
		$treeclass->change_form_visible($params['id'], $_SESSION['login_user']);
	}
	/*	
		Pagal paduotą filtrą ištraukia visas formas atitinkančias kriterijus
		Galimi filtrų operatoriai "privalomas", "negalimas"
		Jei filtras nepaduodamas, grazina visas formas
		
		Jei yra tik negalimas, tai traukia visus ir is ju filtruoja negalimus
		params.filter=[ Array ]
	*/
	function get_forms_filtered($params){
		global $treeclass;			
		if (!isset($params['filter']) || count($params['filter'])<2) return $this->get_forms_all();		
		
		foreach ($params['filter'] as $key=>$value){
			if ((ereg("^[0-9\.]+$", $key)) && (isset($value['field'])) && (isset($value['value'])) && (isset($value['operator'])) &&
				($value['field'] != "") && ($value['value'] != "") && ($value['operator'] != "")) {
					if (trim($value['operator'])=='AND') $new_filter[] = $value;
					else $not_filter[] = $value;
				}
			
		}		
		if (count($new_filter)>0) {
			foreach ($new_filter as $value){
					if (isset($query)) $query.=' OR ';
					$query.= "(`t2`.`name` LIKE '%".$value['field']."' AND `t2`.`value` LIKE '%".$value['value']."')";
			}	
		} else {
			$query="`t1`.`lang`='".$_SESSION['lang']."'";
		}	

		$full_query="
		SELECT DISTINCT
			`t1`.*, `t3`.`name` `block_name`
		FROM 
			`".$this->setupdata['table_prefix']."form_post_catalog` `t1`, `".$this->setupdata['table_prefix']."form_post_data` `t2`, `".$this->setupdata['table_prefix']."form_post_catalog` `t3`
		WHERE 
			`t1`.`lang`='".$_SESSION['lang']."' AND (".$query.") AND `t1`.`id`=`t2`.`parent` AND `t3`.`id`=`t3`.`block_id`
		";				
		
		$sql = $this->mysqlclass->mysql_my_query($full_query, __LINE__, __FILE__);	
		
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {			
			$full_single_form_data = $this->get_forms_data(array('id'=>$row['id']));
			$row['name']=stripslashes($row['name']);
			foreach($full_single_form_data as $value) $row['form_data'][$value['name']]=$value['value'];
			$all_forms[]=$row;				
		}		
		/* atfiltruojam privalomus */
		if (count($new_filter)>0){
			if ((isset($all_forms)) && (count($all_forms)>0)){
				foreach ($all_forms as $form) {
					$meets_requiremens=true;
					foreach ($new_filter as $filter) if(trim($form['form_data'][$filter['field']])!=trim($filter['value'])) $meets_requiremens=false;		
					if ($meets_requiremens==true) $filtered_forms[]=$form;
				}
			}
		} else $filtered_forms=$all_forms;
		
		/* atfiltruojam negalimus */
		if (count($not_filter)>0){
			foreach ($filtered_forms as $form) {
				$meets_requiremens=false;
				foreach ($not_filter as $filter) if(trim($form['form_data'][$filter['field']])==trim($filter['value'])) $meets_requiremens=true;
				if ($meets_requiremens==false) $filtered_forms2[]=$form;
			}
		} else $filtered_forms2=$filtered_forms;
		
		if (count($filtered_forms2)<1) $filtered_forms2=array();		
		return $filtered_forms2;	
	}
	
	function get_forms_all(){
		$arr = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `t1` . * , `t2`.`name` `block_name` FROM `".$this->setupdata['table_prefix']."form_post_catalog` `t1` , `".$this->setupdata['table_prefix']."catalog` `t2` WHERE `t1`.`lang` = '".$_SESSION['lang']."' AND `t2`.`id` = `t1`.`block_id`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			array_push($arr, $row);
		}	
		return $arr;
	}
	
	function get_forms_data($params){
		$arr = array();
		if (isset($params['id']) && strlen($params['id']) == 0) return $arr;
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."form_post_data` WHERE `parent`=".$params['id']." ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = html_entity_decode(stripslashes($row['name']));
			$row['type_of_value'] = $row['type'];
			unset($row['type']);
			$row['value'] = html_entity_decode(stripslashes($row['value']));
			array_push($arr, $row);
		}
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."form_post_catalog` WHERE `id`=".$params['id'], __LINE__, __FILE__);
		$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$arr['parent_info'] = $row;
		return $arr;
	}
	
	function get_form_saved_data_by_form_object_id($params){
		$result = array(); $result2 = array(); $names = array(); $values = array();
		$sql = $this->mysqlclass->mysql_my_query("
			SELECT `t2`.* FROM `".$this->setupdata['table_prefix']."form_post_catalog` `t1`, `".$this->setupdata['table_prefix']."form_post_data` `t2`
			WHERE `t1`.`block_id`=".$params['parent']." AND `t2`.`parent`=`t1`.`id`", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) array_push($result, $row);
		foreach ($result as $key=>$value){
			$values[$value['value']]['name'] = html_entity_decode(stripslashes($value['name']));
			$values[$value['value']]['value'] = html_entity_decode(stripslashes($value['value']));
			$names[$value['name']] = $value['name'];
		}
		$result2['names'] = $names;
		$result2['values'] = $values;
		return $result2;
	}
	
	function get_form_by_parent_id($params){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."form_post_catalog` WHERE `parent`='".$params['page_id']."'", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['id']] = $row;
			$result[$row['id']]['content'] = $this->get_forms_data($row);
		}
		return $result;
	}
		
	function get_form_elements($params){
		global $treeclass;
		$forms = $treeclass->get_form_elem_data($params['parent']);
		return $forms;
	}
	
	function get_page_form($params){
		global $treeclass;
		$type = $params['type'];
		if (strlen($type) > 0){
			$dat=$treeclass->gettreetypes($type);
			foreach ($dat as $value) if ($value['type_of_value'] == 'form') return true;
			return false;
		} else return false;
	}

	function block($params){
		global $treeclass, $smarty;
		$block = $treeclass->get_block_parent($params['id'], $params['name']);
		$smarty->assign("_http_path", $this->setupdata['base_href']);
		$smarty->register_object("ext2", $this->ext2);
		
		$session = $_SESSION;
		if ((isset($_SESSION['login_user']) && (strlen($_SESSION['login_user']) > 0)) || (isset($_GET['show_blocks']))) $smarty->caching = false;
		else unset($session['login_user']);
		
		$cook = $_COOKIE;
		if (isset($cook['tvs_session_id'])) unset($cook['tvs_session_id']);
		if (isset($cook['interface_lang'])) unset($cook['interface_lang']);
		if (isset($cook['shop_id'])) unset($cook['shop_id']);
		if (isset($cook['login_username'])) unset($cook['login_username']);

		$cache_id = md5(serialize(array($params, $_SERVER['REQUEST_URI'], $_GET, $_POST, $session, $cook)));
		
		if ($block['id']) {
			if (isset($treeclass->all_blocks[$block['id']."_".$block['template']])){
				$block = $treeclass->all_blocks[$block['id']."_".$block['template']];
			} else {
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."catalog` WHERE id=".$block['id'], __LINE__, __FILE__);
				$rows = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$block['name'] = stripslashes($rows['name']);
				$block['type'] = $rows['type'];
				$block['data'] = $treeclass->get_item_content($block['id']);
			}
			if ($block['type'] == 'block_vote') $block['can_vote'] = $treeclass->is_can_vote($block['id'], $block['data']);
		}
		if (!isset($block['type'])) $block['type'] = "";
		if (!isset($block['template'])) $block['template'] = "";
		$tempalate_dir = $this->setupdata['smarty_guest_block_template_dir']."/";
		$tempalate_fil = $block['type']."/".$block['template'];
		if (isset($_GET['show_blocks']) && (isset($_SESSION['login_user']))){
			$temp = $smarty->template_dir;
			$smarty->template_dir = $tempalate_dir;
			if ($block['type'] != 'block_php'){
				if ((strlen($tempalate_fil)>1) && ($smarty->template_exists($tempalate_fil)) && (filesize($tempalate_dir.$tempalate_fil) > 0)){
					$smarty->assign("block", $block);
					$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
					$smarty->display($tempalate_fil, $cache_id);
					$smarty->clear_assign("block");
				}
			} else {
				eval($block['data']['block_php_data']['value']);
				$this->mysqlclass->connect_db($this->setupdata['HostName'], $this->setupdata['UserName'], $this->setupdata['Password'], $this->setupdata['DBName']);
			}
			if (!isset($params['template']) or (strlen($params['template']) == 0)) $params['template'] = "-1";
			if (!isset($params['type']) or (strlen($params['type']) == 0)) $params['type'] = "-1";
			$str = "top.openCenteredOnOpenerWindow('index.php?admin=1&id=".$params['id']."&name=".$params['name']."&popup=12&template=".$params['template']."&type=".$params['type']."&lang=".$_SESSION['lang']."','element_new',300,220,1,0);";
			$smarty->assign("url", $str);
			$params['name'] = urldecode($params['name']);
			$smarty->assign("name", $params['name']);
			$tempalate = $this->setupdata['smarty_admin_template_dir']."guest/ibi.html";
			$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
			$smarty->display($tempalate, $cache_id);
			$smarty->clear_assign("url");
			$smarty->clear_assign("name");
			$smarty->template_dir = $temp;
		} else {
			if ($block['type'] != 'block_php'){
				$temp = $smarty->template_dir;
				$smarty->template_dir = $tempalate_dir;
				if ((strlen($tempalate_fil)>1) && ($smarty->template_exists($tempalate_fil)) && (filesize($tempalate_dir.$tempalate_fil) > 0)){
					$smarty->assign("block", $block);
					$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
					$smarty->display($tempalate_fil, $cache_id);
					$smarty->clear_assign("block");
				}
				$smarty->template_dir = $temp;
			} else {
				eval($block['data']['block_php_data']['value']);
				$this->mysqlclass->connect_db($this->setupdata['HostName'], $this->setupdata['UserName'], $this->setupdata['Password'], $this->setupdata['DBName']);
			}
		}
		return false;
	}

	// --- SHOP --- begin
	function get_shop_orders($params){
		$arr = array(); $row = array();	$rows = array();
		if (isset($params['all']))	{
			$sql1 = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart_orders` GROUP BY order_id", __LINE__, __FILE__);
			$sql2 = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart_orders`", __LINE__, __FILE__);
		} elseif ((isset($params['from_date'])) and (isset($params['to_date']))) {
				$sql1 = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart_orders` WHERE UNIX_TIMESTAMP(dattime)>=UNIX_TIMESTAMP(".$params['from_date'].") and UNIX_TIMESTAMP(dattime)<=UNIX_TIMESTAMP(".$params['to_date'].") GROUP BY order_id", __LINE__, __FILE__);
				$sql2 = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart_orders` WHERE UNIX_TIMESTAMP(dattime)>=UNIX_TIMESTAMP(".$params['from_date'].") and UNIX_TIMESTAMP(dattime)<=UNIX_TIMESTAMP(".$params['to_date'].")", __LINE__, __FILE__);
		}
		if (($sql1) && ($sql2)){
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql1, MYSQL_ASSOC)) {
				$rows['id'] = $row['order_id'];
				$rows['client'] = $row['client_id'];
				$rows['date'] = $row['dattime'];
				array_push($arr, $rows);
			}
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql2, MYSQL_ASSOC)) {
				for ($i=0; $i < count($arr); $i++){
					if ($arr[$i]['id'] == $row['order_id']) $arr[$i]['orders'][] = $row;
				}
			}	
		}
		return $arr;
	}

	function get_shop_orders_by($params){
		$arr = array(); $row = array();
		if (isset($params['order'])) $sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart_orders` WHERE order_id =".$params['order'], __LINE__, __FILE__);
		else if (isset($params['client'])) $sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."shop_cart_orders` WHERE client_id =".$params['client'], __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql1, MYSQL_ASSOC)) array_push($arr, $row);
		return $arr;
	}
	
	function clear_cart(){
		global $cart;
		$cart->ClearCart(session_id());
	}
	// --- SHOP --- end
	
	function get_users_fields($params){
		global $treeclass;
		$rows = $treeclass->gettreetypes($params['users']);
		return $rows;
	}
	
	function get_client_fiels($params=""){
		global $treeclass;
		if ($params['user']) $user = $params['user'];
		else $user = $_SESSION['login_user'];
		$tmp = array();
		$mas = array();
		$rows = $treeclass->gettreetypes('client01');
		for ($i=0; $i<count($rows); $i++) {
			$sql = $this->mysqlclass->mysql_my_query("select value FROM `".$this->setupdata['table_prefix']."users_data` where name = '".$rows[$i]['name']."' and parent ='".$user."'", __LINE__, __FILE__);
			$num = $this->mysqlclass->mysql_my_num_rows($sql);
			if ($num > 0) {
				$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				$rows[$i]['value'] = $info['value'];
			} else $rows[$i]['value'] = "";
		}
		return $rows;
	}

	function get_site_settings_id($params=0){
		$rows=array();
		
		if (isset($params['lang'])) $langs=$params['lang'];
		else $langs=$_SESSION['lang'];				
		
		if ((isset($params['format']) && $params['format']!='array') || !isset($params['format'])) {
			$sql = $this->mysqlclass->mysql_my_query("select `id` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type`='site_settings01' and lang='".$langs."'", __LINE__, __FILE__);
			$num = $this->mysqlclass->mysql_my_num_rows($sql);		
			if ($num > 0) {
				$rows = $this->mysqlclass->mysql_my_fetch_array($sql);
				return $rows['id'];
			} else return 0;
		} else {
			$sql = $this->mysqlclass->mysql_my_query("select `id`,`lang`  FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type`='site_settings01'", __LINE__, __FILE__);				
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) $rows[$row['lang']]=$row['id'] ;
			return $rows; 
		}
	}
	
	function get_site_settings(){
		global $treeclass;
		return $treeclass->_get_site_settings();
	}	
	
	function clear_string($params) {
		$params=$params['array'];
		$params=str_replace("\\", "\\\\", $params);
		$params=str_replace("\n", "", $params);
		$params=str_replace("\r", "", $params);
		return $params;
	}
	
	function tree_get_childs($params){
		$mas = array();
		foreach ($params['array'] as $value){
			if ($value['parent']['id'] == $params['id']) {
				array_push($mas, $value);
			} 
		}
		return $mas;
	}
	
	function tree_get_element($params){
		$rows = array();
		if (!isset($params['id'])) return $rows;
		global $treeclass;
		$row = array();
		$id = intval($params['id']);
		$s1 = $treeclass->getchildtypes($id, 'element');
		$s = "('"; foreach ($s1 as $value) $s .= $value['type_of_value']."', '"; $s .= "')";
		if (isset($_GET['admin']) && ($_GET['admin']==1)) $sql_txt = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `parent`=".$id." and `type` IN ".$s." ORDER by pos", __LINE__, __FILE__);
		else $sql_txt = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."catalog` WHERE `parent`=".$id." and `type` IN ".$s." and `hide`=1 ORDER by pos", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql_txt, MYSQL_ASSOC)) {
			if (isset($row['dlink']) && strlen($row['dlink'])>0) {
				if (count($treeclass->active_langs) > 1) $cur_lang = $row['lang']."/";
				else $cur_lang = "";
				if (strpos($row['dlink'], "http://") === false) $row['url'] = $this->setupdata['base_href']."/".$cur_lang.$row['dlink'];
				else $rows['url'] = $row['dlink'];
			} else $row['url'] = $this->setupdata['base_href']."/"."index.php?id=".$row['id'];
			if ($row['type'] == 'url01') {
				$info = $treeclass->get_field_value($row['id'], 'static_url');
				if ((isset($info)) and (strlen($info)>0)) $row['url'] = $info;
			}
			$row['name'] = stripslashes($row['name']);
			array_push($rows, $row);
		}
		return $rows;
	}
	
	function tree_get_element_types($params){
		global $treeclass;			
		return $treeclass->getchildtypes($params['id'], 'element');	
	}

	function tree_get_fields($params){
		global $treeclass;
		$rows2 = array();
		$info = $treeclass->get_item_info($params['id']);
		$rows = $treeclass->gettreetypes($info['type']);
		foreach ($rows as $value) if (($value['group']!='element')&&($value['group']!='comment')) $rows2[]=$value;
		return $rows2;
	}
	
	function get_page_info($params){
		global $treeclass;
		$info = $treeclass->get_item_info($params['id']);
		return $info;
	}

	function get_path (){
		if (isset($this->setupdata['orig_base_href'])) return $this->setupdata['orig_base_href'];
		else return $this->setupdata['base_href'];
	}
	
	function get_referer (){
		return str_replace('=','*',str_replace('&','|',$_SERVER['REQUEST_URI']));
	}
	
	function decode_referer ($params){
		//return str_replace('*','=',str_replace('~','&',$params['referer']));
		return str_replace('|','&',str_replace('*','=',$params['referer']));
	}

	function get_field_value($params){
		$sql = $this->mysqlclass->mysql_my_query("select `value` FROM `".$this->setupdata['table_prefix']."data` where `name` = '".$params['name']."' and parent ='".$params['parent']."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		if (!isset($params['type'])){
			$info['value'] = stripslashes($info['value']);
			return $info['value'];
		} elseif ($params['type'] == 'client_groups'){
			if (class_exists("clientsclass")) {
				$info['value'] = stripslashes($info['value']);
				$group_array = explode(";", $info['value']);
				global $clientsclass;
				$groups = $clientsclass->group_get();
				foreach ($group_array as $key=>$data) {
					if (!array_key_exists($data, $groups)) unset($group_array[$key]);
				}
				$info['value'] = implode(";", $group_array);
			} else {
				$info['value'] = "";
			}
			return $info['value'];
		}
	}

	function get_field_id($params){
		$sql = $this->mysqlclass->mysql_my_query("select id FROM `".$this->setupdata['table_prefix']."data` where name = '".$params['name']."' and parent ='".$params['parent']."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		return $info['id'];
	}

	function get_field_data($params){
		$rows = array();
		$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."data` where parent = '".$params['id']."'", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)) {
			$row['value'] = stripslashes($row['value']);
			array_push($rows, $row);
		}
		return $rows;
	}

	function get_admin_data($params) {
		global $usersclass;
		$row = array();
		$params['id'] = intval($params['id']);
		$row = $usersclass->GetUserInfo($params['id'], "admins");
		return $row;
	}

	function get_type_name($params){
		$sql = $this->mysqlclass->mysql_my_query("select name FROM `".$this->setupdata['table_prefix']."discribe` where type_of_value = '".$params['type']."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		return $info['name'];
	}
	
// suzinom ar cia elementas ar ne. jei elementas neledziam kurti vaiku
	function get_type_group($params){
		$sql = $this->mysqlclass->mysql_my_query("select `group` FROM `".$this->setupdata['table_prefix']."discribe` where type_of_value = '".$params['type']."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		return $info['group'];
	}
	
	function get_binary_data($params){
		
		$id = intval($params['id']);
		
		$tmp = round($id / 250);
		$path_to_cache_file = $this->setupdata['binary_path']."/ids"."/".$tmp."/".$id;
		
		if (file_exists($path_to_cache_file)) {
			require $path_to_cache_file;
			if (isset($data_id)) {
				return $data_id;
			}
		}
		
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."files` WHERE id = '".$id."'", __LINE__, __FILE__);
		$info = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
		$info['info']=unserialize(strval($info['info']));
		
		$path_to_cache_file = $this->setupdata['binary_path']."/ids";
		
		if (!file_exists($path_to_cache_file)) {
			mkdir($path_to_cache_file);
			chmod($path_to_cache_file, 0777);
		}
		
		$path_to_cache_file = $path_to_cache_file."/".$tmp;
		
		if (!file_exists($path_to_cache_file)) {
			mkdir($path_to_cache_file);
			chmod($path_to_cache_file, 0777);
		}
		
		$path_to_cache_file = $path_to_cache_file."/".$id;
		
		$handle = fopen($path_to_cache_file, "w");
		$result = var_export($info, true); 
		fwrite($handle, "<?\n\$data_id = ");
		fwrite($handle, $result);
		fwrite($handle, ";\n?>");
		fclose($handle);
		
		return $info;
		
	}
	
	function paging ($params) {
		if (is_array($params['elements'])) {
			$total_elements=count($params['elements']);
		} else {
			$total_elements=$params['elements'];
		}
		$items_per_page=$params['items_per_page'];
		$current_page=$params['page'];		
		
		if ((!$items_per_page) or ($items_per_page == 0) or ($items_per_page == "")) $items_per_page = 10;
		
		$total_pages=ceil($total_elements/$items_per_page);
		
		if(!isset($current_page) || $current_page=='') $current_page=1;		
		
		for ($i=1; $i<=$total_pages; $i++) {
			$data['pages'][$i]['number']=$i;
			$data['pages'][$i]['from']=$i*$items_per_page-$items_per_page+1;
			$data['pages'][$i]['to']=$i*$items_per_page;
			if($data['pages'][$i]['to']>$total_elements)$data['pages'][$i]['to']=$total_elements;
		}
		
		$data['totalelements']=$total_elements;
		$data['itemsperpage']=$items_per_page;
		$data['totalpages']=$total_pages;
		
		$data['current_page']['number']=$current_page;
		if (isset($data['pages'])){
			if (isset($data['pages'][$current_page]['from'])) $data['current_page']['from']=$data['pages'][$current_page]['from'];
			else $data['current_page']['from']=0;
			if (isset($data['pages'][$current_page]['from'])) $data['current_page']['to']=$data['pages'][$current_page]['to'];
			else $data['current_page']['to']=0;
		} else {
			$data['current_page']['from']=0;
			$data['current_page']['to']=0;
		}
		
		if (isset($data['pages'])){
			if($current_page!=$total_pages && $total_elements!=0) {
				$data['next_page']['number']=$current_page+1;
				if (isset($data['pages'][$current_page+1]['from'])) $data['next_page']['from']=$data['pages'][$current_page+1]['from'];
				else $data['next_page']['from']=0;
				if (isset($data['pages'][$current_page+1]['to'])) $data['next_page']['to']=$data['pages'][$current_page+1]['to'];
				else $data['next_page']['to']=0;
			}
		} else {
			$data['next_page']['number']=0;
			$data['next_page']['from']=0;
			$data['next_page']['to']=0;
		}
		
		if (isset($data['pages'])){
			if($current_page!=1) {
				$data['previous_page']['number']=$current_page-1;
				if (isset($data['pages'][$current_page-1]['from'])) $data['previous_page']['from']=$data['pages'][$current_page-1]['from'];
				else $data['previous_page']['from']=0;
				if (isset($data['pages'][$current_page-1]['to'])) $data['previous_page']['to']=$data['pages'][$current_page-1]['to'];
				else $data['previous_page']['to']=0;
			}
		} else {
			$data['previous_page']['number']=0;
			$data['previous_page']['from']=0;
			$data['previous_page']['to']=0;
		}
		
		return $data;
	}
	
	function get_data($params){
		global $treeclass;
		return $treeclass->get_item_content($params['id']);
	}	

	function get_binary_list($params){
		global $fileclass, $imageclass;
		$id = $params['id'];
		if ($params['type']=='file') $list=$fileclass->file_sel($id);
		elseif($params['type']=='flash') $list=$imageclass->flash_sel($id);
		else $list=$imageclass->image_sel($id);
		foreach ($list as $key=>$value) $list[$key]['info']=unserialize($value['info']);		
		return array_reverse($list);
	}	
	
	function get_binary_list_by_page($params){
		global $fileclass, $imageclass;
		if (isset($params['page'])) $page = $params['page']-1;
		else $page = 0;
		if (isset($params['items_per_page'])) $items_per_page = $params['items_per_page'];
		else $items_per_page = 12;
		$id = $params['id'];
		$paging['curent_page'] = $page; $paging['items_per_page'] = $items_per_page;
		if (isset($params['type'])){
			if ($params['type']=='file') $list=$fileclass->file_sel($id, $paging);
			elseif ($params['type']=='flash') $list=$imageclass->flash_sel($id, $paging);
			else $list=$imageclass->image_sel($id, $paging);
		} else $list=$imageclass->image_sel($id, $paging);
		if (!isset($list['list'])) $list['list'] = array();
		if (!isset($list['count'])) $list['count'] = 0;
		return $list;
	}	
	
	function array_reverse ($params) {
		return array_reverse($params['array']);
	}
	
	/* begin masyvo funkcijos */
	function countArray($params) {		
		if (is_array($params['array']))	return count($params['array']);
		else return 0;
	}		
	
	//rusiuojam daugiamati masyva pagal perduodama elementa
	function aasort($params) { 
		if ((is_array($params['array'])) && (count($params['array']) > 0)){
			$sort_rule = "";
			$params['args']=explode (',', $params['args']);		
			foreach($params['args'] as $arg) { 
				$order_field = substr($arg, 1, strlen($arg)); 
				$sort_array[$order_field] = array();
				foreach($params['array'] as $array_row) { 
					if (isset($array_row[$order_field])) $sort_array[$order_field][] = $array_row[$order_field]; 
					else $sort_array[$order_field][] = NULL;
				}
				if (!is_array($sort_array[$order_field])){
					$sort_array[$order_field] = array_map('strtolower', $sort_array[$order_field]);
				}
				$sort_rule .= '$sort_array[\''.$order_field.'\'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).','; 
			} 
			eval ("array_multisort($sort_rule".' $params[\'array\']);'); 
			return $params['array'];
		} else return NULL;
	} 
			
	/* end masyvo funkcijos */
	
	/* begin - SEKLYS */
	//echo 'y:'.substr($result[$i]['time_modif'], 0,4).' m:'.substr($result[$i]['time_modif'], 5,2).' d:'.substr($result[$i]['time_modif'], 8,2).' h:'.substr($result[$i]['time_modif'], 11,2).' h:'.substr($result[$i]['time_modif'], 14,2).' s:'.substr($result[$i]['time_modif'], 17,2);

	function get_history (){
		global $usersclass;
		$row = array();
		$query="	SELECT `t1`.*, `t2`.`login`
					FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."users` `t2`
					WHERE (`t1`.`type` LIKE 'del_%' || `t1`.`type` LIKE 'cor_%') && `t2`.`id`=`t1`.`editor` && `t1`.`lang`='".$_SESSION['lang']."'
					ORDER BY `t1`.`time_modif`
				";			
		$sql = $this->mysqlclass->mysql_my_query($query, __LINE__, __FILE__);
		for ($i=0; $i<$this->mysqlclass->mysql_my_num_rows($sql); $i++) {
			$result[$i]=$this->mysqlclass->mysql_my_fetch_assoc($sql);
//
			$result[$i]['name'] = stripslashes($result[$i]['name']);
			$row = $usersclass->GetUserInfo($result[$i]['editor'], "admins");
			$result[$i]['first_name']=$row['admins01_fname'];
			$result[$i]['last_name']=$row['admins01_lname'];
			$result[$i]['former_type']=substr($result[$i]['type'], 0, 3);
			$result[$i]['type']=str_replace('del_', '', $result[$i]['type']);
			$result[$i]['type']=str_replace('cor_', '', $result[$i]['type']);
			$result[$i]['time_modif']=mktime( substr($result[$i]['time_modif'], 11,2), substr($result[$i]['time_modif'], 14,2), substr($result[$i]['time_modif'], 17,2), substr($result[$i]['time_modif'], 5,2) , substr($result[$i]['time_modif'], 8,2) , substr($result[$i]['time_modif'], 0,4));
		}
		return $result;
	}
	
	/* end - SEKLYS */
	
	function interval($params){
		$result = array();
		$array = $params['array'];
		if (isset($params['separator'])) $separator = $params['separator'];
		else $separator = "-";
		if (isset($params['count'])) $count = $params['count'];
		else $count = 1;
		foreach ($array as $key=>$value){
			if ($value == "") unset($array[$key]);
		}
		asort($array);
		reset($array);
		$array_count = ceil(count($array) / $count);
		for ($i=0; $i < $count; $i++){
			if (!isset($first)) $first = current($array);
			else $first = next($array);
			$last = $first;
			for ($x=1; $x < $array_count; $x++) {
				$tmp = next($array);
				if ($tmp != "")	$last = $tmp;
			}
			if (isset($first) && isset($last)){
				$result[$i] = $first.$separator.$last;
				unset($last);
			}
		}
		foreach ($result as $key=>$value){
			if ($value == $separator) unset($result[$key]);
		}
		return $result;
	}
	
	function array_key_exists($params){
		if ((isset($params['key'])) && (isset($params['array']))){
			if (array_key_exists($params['key'], $params['array'])) return 1;
			else return 0;
		} else return 0;
	}
	
	function get_select_customer($params){
		$result = array();
		if (isset($params['add'])) $result = explode("||", $params['add']);
		if (isset($params['name'])){
			$sql = $this->mysqlclass->mysql_my_query("select * FROM `".$this->setupdata['table_prefix']."data` WHERE `type`='select_customer' && `name`='".$params['name']."'", __LINE__, __FILE__);
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
				while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
					if (!in_array($row['value'], $result)) array_push($result, $row['value']);
				}
			} else return "";
		} else return "";
		asort($result);
		reset($result);
		return $result;
	}
	
	function get_super_select($params){
		$result = array();
		if (isset($params['name']) && isset($params['parent'])){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id`, `value` FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$params['name']."' AND `group`='super_select' ORDER BY `value` ASC", __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
				$result[$row['id']]['text'] = stripslashes($row['value']);
				$result[$row['id']]['value'] = $row['id'];
			}
			$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."data` WHERE `parent`=".$params['parent']." AND `name`='".$params['name']."' AND `type`='super_select'", __LINE__, __FILE__);			
			if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
				$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
				if (!isset($row['value']) || !isset($result[$row['value']]['text'])){
					$result['selected']['text'] = "";
					$result['selected']['value'] = "";
				} else {
					$result['selected']['text'] = stripslashes($result[$row['value']]['text']);
					$result['selected']['value'] = $row['value'];
				}
			} else {
				$result['selected']['text'] = "";
				$result['selected']['value'] = "";
			}
		}
		return $result;
	}
	
	function get_all_mail_templates(){
		$result = array();
		if (isset($this->setupdata['smarty_guest_mail_template_dir']) && (strlen($this->setupdata['smarty_guest_mail_template_dir']) > 0)){
			if (file_exists($this->setupdata['smarty_guest_mail_template_dir'])) $dir = dir($this->setupdata['smarty_guest_mail_template_dir']);
			else return $result;
		} else {
			if (file_exists("templates/guest/mail")) $dir = dir("templates/guest/mail");
			else return $result;
		}
		while (false !== ($file = $dir->read())) {
			if ($file != '.' && $file != '..') {
				if (!is_dir($file)) $result[$file] = $file;
			}
		}
		$dir->close();
		asort($result);
		return $result;
	}
	
	function get_elements_with_content($params){
		global $treeclass;
		if (!isset($params['id'])) return false;
		if (isset($params['hide']) && ($params['hide'] == 0)) return $treeclass->_get_page_elements_with_content($params['id']);
		else return $treeclass->_get_page_elements_with_content($params['id'], 1);
	}
	
	function GetPostedFormStats($params){
		global $treeclass;
		$result = array();
		$array = $params['array'];
		
		foreach ($array as $key=>$value){
			if (!isset($result[$value['block_id']])){
				$info = $treeclass->get_item_info($value['block_id']);
				$result[$value['block_id']]['id'] = $value['block_id'];
				$result[$value['block_id']]['name'] = $info['name'];
				$result[$value['block_id']]['date'] = $info['time_create'];
				$result[$value['block_id']]['count'] = 1;
			} else {
				$result[$value['block_id']]['count'] = $result[$value['block_id']]['count'] + 1;
			}
		}

		return $result;
	}
	
	function getEditorsAdvanceFuntion(){
		$result = array();
		$result['styles']['plugin'] = "";
		$result['styles']['setup'] = 'theme_advanced_styles: "';
		$result['fontsize']['setup'] = 'theme_advanced_font_sizes : "';
		$result['fonttype']['setup'] = 'theme_advanced_fonts : "';
		$result['fontcolor']['setup'] = 'theme_advanced_more_colors : false,'."\n".'				theme_advanced_text_colors : "';
		
		$file_name = $this->setupdata['smarty_guest_styles_template_file'];
		if (file_exists($file_name)){
			$lines = file($file_name);
			foreach ($lines as $line) {
				$line = trim($line);
				if (strlen($line) > 0){
					$tmp1 = explode("&", $line);
					$tmp2 = explode(":", $tmp1[1]);
					$tmp5 = array();
					foreach ($tmp2 as $tmp3) {
						$tmp4 = explode("=", $tmp3);
						$tmp5[$tmp4[0]] = $tmp4[1];
					}
					$array[$tmp1[0]] = $tmp5;
				}
			}
		}
		
		if (isset($array['site_class']) && (count($array['site_class'])>0)){
			$result['styles']['plugin'] .= "separator,styleselect,";
			foreach ($array['site_class'] as $k=>$v) {
				$result['styles']['setup'] .= $k."=".$v.";";
			}
			$result['styles']['setup'] = substr($result['styles']['setup'], 0, strlen($result['styles']['setup'])-1);
		}

		if (isset($array['site_colors']) && (count($array['site_colors'])>0)){
			$result['styles']['plugin'] .= "separator,forecolor,";
			foreach ($array['site_colors'] as $k=>$v) {
				$result['fontcolor']['setup'] .= str_replace("#", "", $v).",";
			}
			$result['fontcolor']['setup'] = substr($result['fontcolor']['setup'], 0, strlen($result['fontcolor']['setup'])-1);
		}

		if (isset($array['font_size']) && (count($array['font_size'])>0)){
			$result['styles']['plugin'] .= "separator,fontsizeselect,";
			foreach ($array['font_size'] as $k=>$v) {
				$result['fontsize']['setup'] .= str_replace("px", "", $k)."=".str_replace("px", "", $v).",";
			}
			$result['fontsize']['setup'] = substr($result['fontsize']['setup'], 0, strlen($result['fontsize']['setup'])-1);
		}
		
		if (isset($array['font_family']) && (count($array['font_family'])>0)){
			$result['styles']['plugin'] .= "separator,fontselect,";
			foreach ($array['font_family'] as $k=>$v) {
				$result['fonttype']['setup'] .= str_replace("px", "", $k)."=".str_replace("px", "", $v).",";
			}
			$result['fonttype']['setup'] = substr($result['fonttype']['setup'], 0, strlen($result['fonttype']['setup'])-1);
		}
		
		if (strlen($result['styles']['plugin'])>0) $result['styles']['plugin'] = substr($result['styles']['plugin'], 0, strlen($result['styles']['plugin'])-1);
		
		return $result;
	}
	
	function get_print2_translate(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `name`,`value` FROM `".$this->setupdata['table_prefix']."print2` WHERE `lang`='".$_SESSION['lang']."'", __LINE__, __FILE__);
		while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
			$result[$row['name']] = $row['value'];
		}
		return $result;
	}
	
	function save_print2_translate($array, $lang){
		foreach ($array as $k=>$v) {
			$k = base64_decode($k);
			$v = addslashes($v);
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."print2` SET `value`='".$v."' WHERE `name` ='".$k."' AND `lang`='".$lang."'", __LINE__, __FILE__);
		}
	}
	
	function get_one_type_element($params){
		$result = array();
		$id = intval($params['id']);
		$sql = $this->mysqlclass->mysql_my_query("SELECT `type` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id, __LINE__, __FILE__);		
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			$data = array();
			$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.`id`, `t1`.`name`, `t2`.`name` as `parent`  FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."catalog` `t2` WHERE `t1`.`type`='".$row['type']."' AND `t2`.`id`=`t1`.`parent`", __LINE__, __FILE__);
			while ($row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC)){
				$data[$row['parent']][$row['id']] = $row['name'];
			}
		}
		$result = $data;
		return $result;
	}
	
	function translate($params){
		global $translate;
		$uniq = $params['uniq'];
		if ((isset($_COOKIE['interface_lang'])) and ($_COOKIE['interface_lang'] != "")) $interface_lang = $_COOKIE['interface_lang'];
		else $interface_lang = $this->setupdata['default_languages'];	
		if (isset($translate[addslashes($uniq)][strtolower($interface_lang)])){
			$str = stripslashes($translate[addslashes($uniq)][strtolower($interface_lang)]);
		} else {
			$str = $uniq;
		}
		return $str;
	}
	
	function save_sortby($params){
		$params['parent'] = intval($params['parent']);
		if ($params['parent'] == 0) return 0;
		if (($params['sortby'][0] != "+") && ($params['sortby'][0] != "-")) return 0;
		$params['sortby'] = substr($params['sortby'], 0, 20);
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='sortby_".$params['parent']."'", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES ('', 'sortby_".$params['parent']."', '".$params['sortby']."')", __LINE__, __FILE__);
	}
	
	function get_sortby($params){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='sortby_".$params['parent']."'", __LINE__, __FILE__);
		if ($this->mysqlclass->mysql_my_num_rows($sql) > 0){
			$row = $this->mysqlclass->mysql_my_fetch_array($sql, MYSQL_ASSOC);
			return $row['value'];
		} else return "-pos";
	}
}
?>
